import 'package:cunyutech_flutter_ui/src/text/main.dart';
import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

import 'CYConfirmDialog.dart';

class Loading {
  final BuildContext _context;

  Loading(this._context);

  ///打开loading
  void show({Function onClosed}) {
    if(_context == null) return;
    showDialogExt(
      context: _context,
      barrierDismissible: false,
      builder: (context) {
        return SpinKitFadingCircle(color: Colors.white);
      },
    ).then((value) {
      if(onClosed != null) onClosed(value);
    });
  }

  ///关闭loading
  void close() {
    if(_context == null) return;
    Navigator.of(_context).pop();
  }
}


Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
  return FadeTransition(
    opacity: CurvedAnimation(
      parent: animation,
      curve: Curves.easeOut,
    ),
    child: child,
  );
}

class _DialogRoute<T> extends PopupRoute<T> {
  _DialogRoute({
    @required RoutePageBuilder pageBuilder,
    bool barrierDismissible = true,
    String barrierLabel,
    Color barrierColor = const Color(0x80000000),
    Duration transitionDuration = const Duration(milliseconds: 200),
    RouteTransitionsBuilder transitionBuilder,
    RouteSettings settings,
  }) : assert(barrierDismissible != null),
       _pageBuilder = pageBuilder,
       _barrierDismissible = barrierDismissible,
       _barrierLabel = barrierLabel,
       _barrierColor = barrierColor,
       _transitionDuration = transitionDuration,
       _transitionBuilder = transitionBuilder,
       super(settings: settings);

  final RoutePageBuilder _pageBuilder;

  @override
  bool get barrierDismissible => _barrierDismissible;
  final bool _barrierDismissible;

  @override
  String get barrierLabel => _barrierLabel;
  final String _barrierLabel;

  @override
  Color get barrierColor => _barrierColor;
  final Color _barrierColor;

  @override
  Duration get transitionDuration => _transitionDuration;
  final Duration _transitionDuration;

  final RouteTransitionsBuilder _transitionBuilder;

  @override
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
    return Semantics(
      child: _pageBuilder(context, animation, secondaryAnimation),
      scopesRoute: true,
      explicitChildNodes: true,
    );
  }

  @override
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
    if (_transitionBuilder == null) {
      return FadeTransition(
          opacity: CurvedAnimation(
            parent: animation,
            curve: Curves.linear,
          ),
          child: child);
    } // Some default transition
    return _transitionBuilder(context, animation, secondaryAnimation, child);
  }
}

class CYLoading {
  ThemeData theme;
  BuildContext context;
  CYLoading({
    this.context
  }) {
    theme = Theme.of(context, shadowThemeOnly: true);
  }
  ///打开loading
  void show({Function onClosed}) {
    Navigator.of(context).push(_DialogRoute(
     pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
      final Widget pageChild = SpinKitFadingCircle(color: Colors.white);
        return SafeArea(
          child: Builder(
            builder: (BuildContext context) {
              return theme != null
                  ? Theme(data: theme, child: pageChild)
                  : pageChild;
            }
          ),
        );
      },
  
      barrierDismissible: false,
      barrierColor: Colors.black26,
      transitionDuration: const Duration(milliseconds: 150),
      transitionBuilder: _buildMaterialDialogTransitions,
    ));
  }

  ///关闭loading
  void close() {
    Navigator.of(context).pop();
  }
}


class LoadingV2 {
  bool opened = false;
  BuildContext rootContext;

  LoadingV2({
    this.rootContext
  });

  ///打开loading
  void show({
    Function onClosed,
    String title,
  }) {
    if(opened == true) return;
    opened = true;
    BuildContext context = rootContext;
    var cs = CYThemeData.of(context);
    WidgetBuilder builder = cyThemeWrapBuilder(
      (BuildContext _) {
        final Widget pageChild = SpinKitFadingCircle(color: Colors.white);
        Widget content = Container(
          alignment: Alignment.center,
          child: Container(
            width: 128,
            height: 128,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Color(0xFF101214),
              borderRadius: BorderRadius.circular(CYBorderRadius.Large) 
            ),
            padding: EdgeInsets.symmetric(horizontal: CYMargin.Small),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                pageChild,
                title == null || title.isEmpty ? null : CYParagraph(title, color: cs.darkForeColorSystem.primaryColor, textAlign: TextAlign.center, alignment: Alignment.center, lineHeight: null, margin: EdgeInsets.only(top: CYMargin.Default),)
              ].where((item)=> item != null).toList()
            ),
          )
        );
        return content;
      },
      context,
    );

    NavigatorState navigatorState = Navigator.of(context);
    navigatorState.push(_DialogRoute(
      pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
        return SafeArea(
          child: Builder(
            builder: builder
          ),
        );
      },
      barrierDismissible: false,
      barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
      barrierColor: Color(0x01000000),
      transitionDuration: const Duration(milliseconds: 100),
      transitionBuilder: _buildMaterialDialogTransitions,
    ));
  }

  ///关闭loading
  void close() {
    if(opened == false) return;
    BuildContext context = rootContext;
    NavigatorState navigatorState = Navigator.of(context);
    opened = false;
    navigatorState.pop();
  }
}